package com.runen.wnhz.runen.ui.fragment.major;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.bean.LessonLidBean;
import com.runen.wnhz.runen.data.entity.CatalogListEntity;
import com.runen.wnhz.runen.data.entity.LessonIntroductionEntity;
import com.runen.wnhz.runen.data.entity.TestListEntity;
import com.runen.wnhz.runen.di.component.ApplicationComponent;
import com.runen.wnhz.runen.di.component.DaggerCourseDetailsComponent;
import com.runen.wnhz.runen.di.module.CourseDetailsModule;
import com.runen.wnhz.runen.presenter.Contart.CourseDetailsContart;
import com.runen.wnhz.runen.presenter.iPresenter.ICourseDetailsPresenter;
import com.runen.wnhz.runen.ui.adapter.puadapter.TeachAssistanceRecyclerAdapter;
import com.runen.wnhz.runen.ui.fragment.BaseFragment;
import com.runen.wnhz.runen.widget.TitleBuilder;

import org.simple.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 课程简介
 * Created by Administrator on 2018-04-12.
 */

public class IntroduceFragment extends BaseFragment<ICourseDetailsPresenter> implements CourseDetailsContart.View {
    public static final String TAG = "IntroduceFragment";

    @BindView(R.id.tv_use_mans)
    TextView tvUseMans;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_learn)
    TextView tvLearn;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_discount)
    TextView tvDiscount;
    @BindView(R.id.tv_des)
    TextView tvDes;
    @BindView(R.id.rb_introduce)
    RatingBar rbIntroduce;
    @BindView(R.id.ll_material)
    LinearLayout llMaterial;
    @BindView(R.id.teachAssistantRecycler)
    RecyclerView teachAssistantRecycler;
    Unbinder unbinder;

    /**
     * Lesson_formIntroduction需要的参数
     *
     * @param token 可为空
     * @param lid   必填
     * @return
     */
    public static IntroduceFragment newInstance(String token, String lid) {
        IntroduceFragment fragment = new IntroduceFragment();
        Bundle bundle = new Bundle();
        bundle.putString("token", token);
        bundle.putString("lid", lid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void setupAcitivtyComponent(ApplicationComponent appComponent) {
        DaggerCourseDetailsComponent.builder().applicationComponent(appComponent)
                .courseDetailsModule(new CourseDetailsModule(this))
                .build().inject(this);
    }

    @Override
    protected void init() {
        super.init();
        Bundle bundle = getArguments();
        if (bundle != null) {
            Log.e(TAG, (mPresenter == null) + "");
            mPresenter.getLessonIntro(bundle.getString("token"), bundle.getString("lid"));
        }
    }

    @Override
    protected int getLayoutView() {
        return R.layout.introduce_fragment_layout;
    }

    @Override
    public void initUiAndListener(View view) {

    }

    @Override
    public void initToolBar(TitleBuilder mTitleBuilder) {
        setToolBarVisible(View.GONE);
    }

    @Override
    public void showLoadding() {
    }

    @Override
    public void dimissLoding() {
    }

    @Override
    public void showErrorMessage(String msg) {

    }


    @Override
    public void onTitleButtonClicked(TitleBuilder.TitleButton clicked) {

    }

    @Override
    public void getLessonIntroRequest(LessonIntroductionEntity data) {
        tvDes.setText(data.getDes());
        tvTitle.setText(data.getTitle());
        tvDiscount.setText(data.getDiscount());
        tvLearn.setText(data.getLearn_man() + "人在学");
        tvUseMans.setText(data.getUse_mans());
        tvPrice.setText("￥" + data.getPrice());
        rbIntroduce.setRating(Float.valueOf(data.getStar()));
        if(data.getWant_learn() != null) {
            EventBus.getDefault().post(data.getWant_learn());
        }
        LessonLidBean lessonLidBean = new LessonLidBean();
        lessonLidBean.setLessonLid(data.getLid());
        lessonLidBean.setListBeanList(data.getMaterialList());
        EventBus.getDefault().post(lessonLidBean);
        if ("1".equals(data.getHave_file())) { //have_file 1 为包含资料， 0 为不包含资料
            llMaterial.setVisibility(View.VISIBLE);
            teachAssistantRecycler.setLayoutManager(new GridLayoutManager(getActivity(),3));
            teachAssistantRecycler.setAdapter(new TeachAssistanceRecyclerAdapter(data.getMaterialList(),getActivity()));
        } else {
            llMaterial.setVisibility(View.GONE);
        }
    }

    @Override
    public void getLessonTestRequest(TestListEntity data) {

    }


    @Override
    public void getCatalogRequest(List<CatalogListEntity> data) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
